package com.nhxcz.bigdataweb.controller;

import com.nhxcz.bigdataweb.entity.User;
import com.nhxcz.bigdataweb.mapper.UserMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/login")
    public boolean login(@Param("userName") String userName, @Param("userPassword") String userPassword) {
        System.out.println(userName + " " + userPassword);
        User user = userMapper.getUser(userName);
        if (user == null) return false;
        return user.getUser_password().equals(userPassword) ? true : false;
    }

    @GetMapping("/register")
    public boolean addUser(@Param("userName") String userName, @Param("userPassword") String userPassword) {
        List<User> users = userMapper.getUsers();
        for (User u : users)
            if (u.getUser_name().equals(userName))
                return false;

        userMapper.addUser(userName, userPassword);

        return true;
    }
}
